package io.zt.dangyuan;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;

import java.util.HashMap;

import io.zt.dangyuan.util.Cons;
import io.zt.dangyuan.util.MyCallBack;
import io.zt.dangyuan.util.XUtil;

/**
 * Created by Administrator on 2015/7/6.
 */
public class WebUtils {
    public static final String WEB_URL = Cons.BASE_URL+"/api/";
    public static boolean showProgress = true;

    private static boolean isGooging = false;


    public interface OnWebListenr {

        void onSuccess(boolean isSuccess, String msg, String data);


        void onFailed(HttpException error, String msg);
    }

    //不显示等待进度条，为了不影响之前代码，重写
    public static void sendPostNoProgress(final Context c, String method, HashMap<String, Object> map, final OnWebListenr listen) {
        showProgress = false;
        sendPost(c, method, map, listen);
    }

    public static void sendPost(final Context c, String method, HashMap<String, Object> jsonMap, final OnWebListenr listen) {
        RequestParams params = new RequestParams();
        StringBuffer buf = new StringBuffer("?");
        int index = 0;
        for (String key : jsonMap.keySet()) {


            index++;
            buf.append(key);
            buf.append("=");
            buf.append(jsonMap.get(key));

            if (index == jsonMap.size()) {

            } else {
                buf.append("&");
            }
        }
        String url = WEB_URL + method + ".json" + buf.toString();
        Log.e("tag",url);
        XUtil.Post(url,new HashMap<String,Object>(),new MyCallBack<String>()
        {
                    @Override
                    public void onCancelled(CancelledException cex) {
                        System.out.print("取消");
                    }
                    @Override
                    public void onFinished() {
                        System.out.print("完成");
                    }
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        if (result == null) {
                            Toast.makeText(c, "服务器出现故障", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        ResultBaseModel baseModel = JSON.parseObject(
                                (String)result, ResultBaseModel.class);
                        if (listen != null) {
                            listen.onSuccess(baseModel.isSuccess() ? true : false, baseModel.getMessage(), baseModel.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        System.out.print(ex.getMessage().toString());
                    }
                });
    }


    public static void sendGet(final Context c, String method, HashMap<String, Object> jsonMap, final OnWebListenr listen) {
        RequestParams params = new RequestParams();
        StringBuffer buf = new StringBuffer("?");
        int index = 0;
        for (String key : jsonMap.keySet()) {


            index++;
            buf.append(key);
            buf.append("=");
            buf.append(jsonMap.get(key));

            if (index == jsonMap.size()) {

            } else {
                buf.append("&");
            }
        }
        String url = WEB_URL + method + ".json" + buf.toString();
        Log.e("tag",url);
        XUtil.Get(url,new MyCallBack<String>()
        {
            @Override
            public void onCancelled(CancelledException cex) {
                System.out.print("取消");
            }
            @Override
            public void onFinished() {
                System.out.print("完成");
            }
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                if (result == null) {
                    Toast.makeText(c, "服务器出现故障", Toast.LENGTH_SHORT).show();
                    return;
                }
                ResultBaseModel baseModel = JSON.parseObject(
                        (String)result, ResultBaseModel.class);
                if (listen != null) {
                    listen.onSuccess(baseModel.isSuccess() ? true : false, baseModel.getMessage(), baseModel.getData());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                System.out.print(ex.getMessage().toString());
            }
        });
    }

    public static boolean isConnect(Context context) {
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        try {
            ConnectivityManager connectivity = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                // 获取网络连接管理的对象
                NetworkInfo info = connectivity.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    // 判断当前网络是否已经连接
                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {

        }
        return false;
    }
}
